package com.opencee.cloud.uaa.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

/**
 * @author yadu
 */
@Slf4j
@Component
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent event) {
        //这里的事件源除了登录事件（UsernamePasswordAuthenticationToken）还有可能是token验证事件源（OAuth2Authentication）
        if (!(event.getSource() instanceof UsernamePasswordAuthenticationToken)) {
            return;
        }
        //这里还有oAuth2的客户端认证的事件，需要做一个判断
        if (event.getAuthentication().getDetails() != null) {
            //登录日志逻辑。。。
            log.info(event.toString());
        }
    }
}
